<?php
class BbsAccessException extends WException {}		//	잘못된접근
class BbsPermissionException extends WException {}	//	접근 권한이 없음.
class Cl_Bbs extends WdkClass 
{
	protected
		/******************************************************/
		//	원본 테이블 정보
		/******************************************************/
		$bbs_seq				= 0			,	//
		$bbs_id					= ''		,	//	
		$bbs_name				= ''		,	//	
		$bbs_skin				= 'default'	,	//		
		$bbs_use_flag			= 'T'		,	//	
		$bbs_listsize			= 15		,	//	
		$bbs_pagesize			= 10		,	//	
		$bbs_header				= ''		,	//	
		$bbs_footer				= ''		,	//
		//	추가 옵션 설정 필드들..
		$bbs_notice_flag		= 'T'		,	//	
		$bbs_comment_flag		= 'T'		,	//	
		$bbs_recommend_flag		= 'T'		,	//		
		$bbs_secure_flag		= 'F'		,	//	
		$bbs_captcha_flag		= 'F'		,	//	
		$bbs_scrap_flag			= 'T'		,	//	
		$bbs_linked_post_flag	= 'T'		,	//	
		$bbs_moveup_flag		= 'F'		,	//	
		$bbs_poll_flag			= 'F'		,	//	
		$bbs_report_flag		= 'T'		,	//	
		$bbs_copy_flag			= 'F'		,	//	
		$bbs_tag_flag			= 'T'		,	//	
		//	업로드 설정 필드들..	
		$bbs_file_flag			= 'F'		,	//	
		$bbs_file_num			= 1			,	//	
		$bbs_file_size			= 2			,	//	
		$bbs_file_type			= '*'		,	//	
		//	권한/포인트 설정 		
		$bbs_list_level			= 0			,	//	
		$bbs_read_level			= 0			,	//	
		$bbs_write_level		= 0			,	//
		$bbs_write_point		= 0			,	//	
		$bbs_read_point			= 0			,	//	
		$bbs_comment_point		= 0			,	//
		//	카테고리 설정
		$bbs_category_flag		= 'F'		,	//
		//	추가 필드	
		$bbs_option_fields		= array()	,	//
		//	관리자 설정	
		$bbs_admin_seq			= ''		,	//	
		$bbs_insert_time		= 0			,	//	
		$bbs_del_flag			= 'F'		,	//	
		/******************************************************/
		//	추가 정보
		/******************************************************/
		$categories				= array()	,	//	
		$admin_users			= array()	;	//	
	
	/**
	 * 	
	 */
	function __construct()
	{
		parent::__construct();
		self::getDbo();
	}
	
	/**
	 *	
	 */
	public function __init($data)
	{
		parent::__init($data);
		self::dformat();
	}
	
	/**
	 *	
	 */
	public function load($bbs_seq)
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		/*------------------------------------------------------------*/
		self::__init($dbo->cLoad($bbs_seq));
	}
	
	/**
	 *	
	 */
	public function loadById($bbs_id)
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		/*------------------------------------------------------------*/
		self::__init($dbo->cLoadById($bbs_id));
	}
	
	/**
	 *	
	 */
	public function setUserSession($session)
	{
		$this->session = $session;
	}
	public function isAdmin()
	{
		return false;
	}
	public function isListable()
	{
		if($this->isAdmin()
		||(int) $this->session['user_group_level'] >= $this->bbs_list_level)
		{
			return true;
		}
		return false;
	}
	public function isReadable()
	{
		if($this->isAdmin()
		||(int) $this->session['user_group_level'] >= $this->bbs_read_level)
		{
			return true;
		}
		return false;
	}
	public function isWritable()
	{
		if($this->isAdmin()
		||(int) $this->session['user_group_level'] >= $this->bbs_write_level)
		{
			return true;
		}
		return false;
	}
	
	public function setCategory($seqs=array(), $names=array(), $indexes=array(), $use_flags=array(), $del_flags=array())
	{
		$seqs 		= (array) $seqs;
		$names		= (array) $names;
		$indexes	= (array) $indexes;
		$use_flags	= (array) $use_flags;
		$del_flags	= (array) $del_flags;
		
		foreach($seqs as $idx=>$seq)
		{
			if(trim($names[$idx]) == '') continue;
			$cate = Wdk::get('Category');
			try
			{
				if($seq > 0)
				{
					$cate->load($seq);
					if($del_flags[$idx] == 'T')
					{
						$cate->delete();
						continue;
					}
				}
			}catch(NoRecordException $e){}
			$cate['fk_bbs_seq'] = $this->bbs_seq;
			$cate['category_name'] = $names[$idx];
			$cate['category_index'] = $indexes[$idx];
			$cate['category_use_flag'] = $use_flags[$idx];
			$cate->autoUpdate();
		}
	}
	
	/**
	 *	
	 */
	public function dformat()
	{
		$cm = Wdk::get('CategoryManager');
		$cm->loadByBBsSeq($this->bbs_seq);
		foreach($cm['lists'] as $cate)
		{
			$this->categories[] = $cate;
		}
		
		if(is_string($this->bbs_option_fields))
		{
			$this->bbs_option_fields = unserialize($this->bbs_option_fields);
			$this->option_fields_format();
		}
		
		$this->admin_users = explode(';', $this->bbs_admin_seq);
	}
	
	private function option_fields_format()
	{
		if(empty($this->bbs_option_fields) == false && array_key_exists('must', $this->bbs_option_fields))
		{
			$option_fields = array();
			foreach($this->bbs_option_fields['must'] as $idx=>$tmp)
			{
				$option_fields[] = array(	'must'		=>	$this->bbs_option_fields['must'][$idx],
											'type'		=>	isset($this->bbs_option_fields['type'][$idx]) ? $this->bbs_option_fields['type'][$idx] : 'input',
											'name'		=>	$this->bbs_option_fields['name'][$idx],
											'values'	=>	$this->bbs_option_fields['values'][$idx],
											'values_arr'=>	explode(";", $this->bbs_option_fields['values'][$idx])					
										);
			}
			$this->bbs_option_fields = $option_fields;
		}
	}
	
	/**
	 * 
	 */
	public function autoUpdate($log='')
	{
		if((int)$this->bbs_seq > 0)
		{
			$this->update($log);
		}
		else
		{
			$this->insert($log);
		}
	}
	
	/**
	 *	
	 */
	public function insert($log='Insert')
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		/*------------------------------------------------------------*/
		
		$this->bbs_insert_time	= time();
		$this->bbs_del_flag		= 'F';
		
		$data = array(	'bbs_id'				=>	$this->bbs_id,
						'bbs_name'				=>	$this->bbs_name,
						'bbs_skin'				=>	$this->bbs_skin,
						'bbs_use_flag'			=>	$this->bbs_use_flag,
						'bbs_listsize'			=>	$this->bbs_listsize,
						'bbs_pagesize'			=>	$this->bbs_pagesize,
						'bbs_header'			=>	$this->bbs_header,
						'bbs_footer'			=>	$this->bbs_footer,
						'bbs_notice_flag'		=>	$this->bbs_notice_flag,
						'bbs_comment_flag'		=>	$this->bbs_comment_flag,
						'bbs_recommend_flag'	=>	$this->bbs_recommend_flag,
						'bbs_secure_flag'		=>	$this->bbs_secure_flag,
						'bbs_captcha_flag'		=>	$this->bbs_captcha_flag,
						'bbs_scrap_flag'		=>	$this->bbs_scrap_flag,
						'bbs_linked_post_flag'	=>	$this->bbs_linked_post_flag,
						'bbs_moveup_flag'		=>	$this->bbs_moveup_flag,
						'bbs_poll_flag'			=>	$this->bbs_poll_flag,
						'bbs_report_flag'		=>	$this->bbs_report_flag,
						'bbs_copy_flag'			=>	$this->bbs_copy_flag,
						'bbs_tag_flag'			=>	$this->bbs_tag_flag,
						'bbs_file_flag'			=>	$this->bbs_file_flag,
						'bbs_file_num'			=>	$this->bbs_file_num,
						'bbs_file_size'			=>	$this->bbs_file_size,
						'bbs_file_type'			=>	$this->bbs_file_type,
						'bbs_list_level'		=>	$this->bbs_list_level,
						'bbs_read_level'		=>	$this->bbs_read_level,
						'bbs_write_level'		=>	$this->bbs_write_level,
						'bbs_write_point'		=>	$this->bbs_write_point,
						'bbs_read_point'		=>	$this->bbs_read_point,
						'bbs_comment_point'		=>	$this->bbs_comment_point,
						'bbs_category_flag'		=>	$this->bbs_category_flag,
						'bbs_option_fields'		=>	serialize((array)$this->bbs_option_fields),
						'bbs_admin_seq'			=>	$this->bbs_admin_seq,
						'bbs_insert_time'		=>	$this->bbs_insert_time,
						'bbs_del_flag'			=>	$this->bbs_del_flag
					);
		$this->bbs_seq = $dbo->insert($data);
		/******************************************************/
		parent::setBackupData(array_keys($data));
		$this->backup($log);
		/******************************************************/
		$this->option_fields_format();
	}
	
	/**
	 * 
	 */
	public function update($log='update')
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		/*------------------------------------------------------------*/
		
		if(parent::isUpdatable())
		{
			$data = array(	'bbs_name'				=>	$this->bbs_name,
							'bbs_skin'				=>	$this->bbs_skin,
							'bbs_use_flag'			=>	$this->bbs_use_flag,
							'bbs_listsize'			=>	$this->bbs_listsize,
							'bbs_pagesize'			=>	$this->bbs_pagesize,
							'bbs_header'			=>	$this->bbs_header,
							'bbs_footer'			=>	$this->bbs_footer,
							'bbs_notice_flag'		=>	$this->bbs_notice_flag,
							'bbs_comment_flag'		=>	$this->bbs_comment_flag,
							'bbs_recommend_flag'	=>	$this->bbs_recommend_flag,
							'bbs_secure_flag'		=>	$this->bbs_secure_flag,
							'bbs_captcha_flag'		=>	$this->bbs_captcha_flag,
							'bbs_scrap_flag'		=>	$this->bbs_scrap_flag,
							'bbs_linked_post_flag'	=>	$this->bbs_linked_post_flag,
							'bbs_moveup_flag'		=>	$this->bbs_moveup_flag,
							'bbs_poll_flag'			=>	$this->bbs_poll_flag,
							'bbs_report_flag'		=>	$this->bbs_report_flag,
							'bbs_copy_flag'			=>	$this->bbs_copy_flag,
							'bbs_tag_flag'			=>	$this->bbs_tag_flag,
							'bbs_file_flag'			=>	$this->bbs_file_flag,
							'bbs_file_num'			=>	$this->bbs_file_num,
							'bbs_file_size'			=>	$this->bbs_file_size,
							'bbs_file_type'			=>	$this->bbs_file_type,
							'bbs_list_level'		=>	$this->bbs_list_level,
							'bbs_read_level'		=>	$this->bbs_read_level,
							'bbs_write_level'		=>	$this->bbs_write_level,
							'bbs_write_point'		=>	$this->bbs_write_point,
							'bbs_read_point'		=>	$this->bbs_read_point,
							'bbs_comment_point'		=>	$this->bbs_comment_point,
							'bbs_category_flag'		=>	$this->bbs_category_flag,
							'bbs_option_fields'		=>	serialize((array)$this->bbs_option_fields),
							'bbs_admin_seq'			=>	$this->bbs_admin_seq
					);
			$dbo->update($data, array("bbs_seq = ".$this->bbs_seq));
			/******************************************************/
			parent::setBackupData(array_keys($data));
			$this->backup($log);
			/******************************************************/
			$this->option_fields_format();
		}
	}
	
	/**
	 * 
	 */
	public function delete($log='delete')
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		/*------------------------------------------------------------*/
		$dbo->delete(array("bbs_seq = ".$this->bbs_seq));
		/******************************************************/
		parent::setBackupData();
		$this->backup($log);
		/******************************************************/
	}
	
	/**
	 *	
	 */
	private function backup($log='')
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		/*------------------------------------------------------------*/
		$dbo->log($this->bbs_seq, $log, $this->backup_data);
	}
	
	/**
	 * 
	 */
	public function getDbo()
	{
		return Wdk::model(substr(__CLASS__, 3));
	}
}